<?php

namespace App\Http\Controllers\Admin\Editor;
use App\Http\Requests\ArticlesPostRequest;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BooksController extends Controller
{
    public function getIndex(Request $request)
    {
        $books = DB::table('hs_books')
                    ->join('hs_member','hs_member.id','=','hs_books.aid')
                    ->join('hs_relation','hs_relation.aid','=','hs_member.id')
                    ->join('hs_editor','hs_editor.id','=','hs_relation.eid')
                    ->where(['hs_editor.id'=>$request->session()->get('id'),'hs_books.status'=>'0'])
                    ->select('hs_books.id','hs_books.isfree','hs_books.status','hs_member.name','hs_books.b_name','hs_books.b_pic','hs_books.b_desc','hs_books.isend','hs_books.b_cate')
                    ->paginate(2);
                    // ->get();
        // dd($books);
        return view('admin.editor.book.index',['books'=>$books]);      
    }

    public function getYindex(Request $request)
    {
        $books = DB::table('hs_books')
                    ->join('hs_member','hs_member.id','=','hs_books.aid')
                    ->join('hs_relation','hs_relation.aid','=','hs_member.id')
                    ->join('hs_editor','hs_editor.id','=','hs_relation.eid')
                    ->where(['hs_editor.id'=>$request->session()->get('id'),'hs_books.status'=>'1'])
                    ->select('hs_books.id','hs_books.isfree','hs_books.status','hs_member.name','hs_books.b_name','hs_books.b_pic','hs_books.b_desc','hs_books.isend','hs_books.b_cate')
                    ->paginate(2);
                    // ->get();
        // dd($books);
        return view('admin.editor.book.index',['books'=>$books]);    
    }

    public function getAllow($id,$status)
    {
    	// return 'adsd';
       
       $res = DB::table('hs_books')->where('id',$id)->update(['status'=>$status]);
       if($res){
            return redirect('/admin/ebooks/yindex')->with('success','操作成功');
       }else{
            return redirect('/admin/ebooks/yindex')->with('error','操作失败');
       }
    }

    public function getEdit($id)
    {
    	$book = DB::table('hs_books')->where('id',$id)->first();
        return view('admin.editor.book.edit',['book'=>$book]);
    }

     public function postUpdate(Request $request)
    {
    	$id = $request->input('id');
        $data['b_cate'] = $request->input('isend');
        if($request->hasFile('pic')){
            $ext = $request->file('pic')->getClientOriginalExtension();
            $filename = time().str_random(5).'.'.$ext;
            // dd($filename);
            $data['b_pic'] = '/uploads/books'.'/'.$filename;
            $request->file('pic')->move('./uploads/books/', $filename);
        }
        $res = DB::table('hs_books')->where('id','=',$id)->update($data);
        //判断更新数据是否成功
        if($res){
            return redirect('/admin/ebooks/index')->with('success','更新书籍成功');
        }else{
            return back()->with('error','更新书籍失败');
        }
    }

}
